
/////////////////////////////////////////////
// System global define
/////////////////////////////////////////////

#define true            1
#define false           0




enum Device_Enable
{
  DeviceOff,
  DeviceOn
};

enum For_G_SysModeStatusCode
{
  UnKnowStatus,
  StartUp,
  AssemblyMode,
  AssemblyModeReady,
  NormalMode,
  FailShutdownMode,
  ShutdownMode,
  CalibrationMode
};

enum Calibration_Status
{
  Charging,
  Discharging,
  First_Cal,
  Second_Cal
};



/* Module Status Control Bits */
#define SC0          				(0x0001)    //No Use, Status Code, returns status of processed SBS function
#define SC1          				(0x0002)    //No Use, Status Code, returns status of processed SBS function
#define SC2          				(0x0004)    //No Use, Status Code, returns status of processed SBS function
#define SC3          				(0x0008)    //No Use, Status Code, returns status of processed SBS function

#define CHG          				(0x0001)    //temp value for main function
#define UTA        				  (0x0002)    //Under Temperature Alarm ,
#define SOH1         				(0x0004)    //SOH1 Status Code,
#define SOH2         				(0x0008)    //SOH2 Status Code,
#define S1       		 				(0x0010)    //First series block occurs Failure                   
#define S2       		 				(0x0020)    //Second series block occurs Failure                  
#define S3       		 				(0x0040)    //Third series block occurs Failure                   
#define C_OC     			      (0x0080)    //Charge over current 
#define D_OC                (0x0100)    //DisCharge over current 
#define FD					        (0x0200)    //Fully Discharged                                                             
#define FC					        (0x0400)    //Fully Charged                                                                
#define DSG				          (0x0800)    //Discharging, 0 = in charging mode,1 = in discharging mode or relaxation mode.
#define TDA				          (0x1000)    //Terminate Discharge Alarm                                                    
#define OTA			            (0x2000)    //Over Temperature Alarm                                                       
#define TCA				          (0x4000)    //Terminate Charge Alarm                                                       
#define OCA				          (0x8000)    //Over Charged Alarm
//for extend in main function
#define Module_OT           (0x2000)    //=OTA, Over Temperature Alarm 
#define Module_D_OC         (0x0100)    //=D_OC, DisCharge over current 
#define Module_C_OC         (0x0080)    //=C_OC, Charge over current 
#define Module_UV           (0x1000)    //=TDA, Terminate Discharge Alarm
#define Module_OV           (0x8000)    //=OCA, Over Charged Alarm
#define Module_DSG          (0x0800)    //=DSG, Discharging, 0 = in charging mode,1 = in discharging mode or relaxation mode.
#define Module_CHG          (0x0001)    //=CHG,
#define Module_TCA          (0x4000)    //=TCA, Terminate Charge Alarm


/* Indication Failure Packs to in fail shutdown mode */
#define PermittingPackFailureNumOfModule  1 
/* Indication_FailureMode_Status in fail shutdown mode
   define FailureMode_Status bit */
#define NoneFail            (0x00)    //
#define AbnormalFail				(0x01)    //
#define MOSFETFail   				(0x02)    //
#define NBPCFail     				(0x04)    //
#define ADCFail     				(0x08)    //
#define OCFail  		 				(0x10)    //
//#define no     		 				(0x20)    //
//#define no     		 				(0x40)    //
//#define no     			      (0x80)    //

////////////////////////////////////////////////////////////////////////////
//Main function
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//System Information configuration
////////////////////////////////////////////////////////////////////////////
void InitSystemInfo();
void setCurrentToArray( int current);
unsigned int getSystemVoltage();
int getSystemCurrent();
int getAvgSystemCurrent();
void setPackCommunicationFailure(char moduleName, char packName, char failureFlag);
void setPackSOHFailure(char moduleName, char packName, char failureFlag);
void setPackFCCFailure(char moduleName, char packName, char failureFlag);
void setPackCycleCountFailure(char moduleName, char packName, char failureFlag);
void setPackFETStatus(char moduleName, char packName, unsigned char FETControl);
char isSystemNoPackFail();
char CheckSystemFailure();
void CheckSystemOnePackFailureForLEDBlinking();


////////////////////////////////////////////////////////////////////////////
//PackMosFETControl configuration
////////////////////////////////////////////////////////////////////////////
char setPackMosFET(char moduleName, char enable);
void PackMosFETControlAtChangingNewOne( char packNum, unsigned char Out_moduleOfPackChangedNewOneBit );
char isPackFETTurnOn_ByManufacturerAccess(char moduleName, char packNum);
void CheckingPackMosFETAbnormalStatus( char packNum );


////////////////////////////////////////////////////////////////////////////
// Utility Functions at System Information configuration file
int calculateSystemCurrent();
unsigned int calculateSystemVoltage();
int getModuleCurrent(char module_Num);
int getModuleVoltage(char module_Num);

////////////////////////////////////////////////////////////////////////////
//EEPROM Data Access configuration 
// 
////////////////////////////////////////////////////////////////////////////
void initEEPROMDataAccess();
void TransferArrayPerOneDay(unsigned int DayCounts);
char WriteInitDataToEEPROM();
char ReadEEPROMDataIndex();
char SaveEEPROMPackData();
char SaveEEPROMCurrentData();
void resetOneDayData();
void resetTwoDaysData();
void resetSixDaysData();
void updatedForSavingMaxMinDataToEEPROM();
char ReadEEPROMPackData(unsigned int Item_index, char command, unsigned char *buffer);
char ReadEEPROMCurrentData(unsigned int Item_index, char command, unsigned char *buffer);
char ReadEEPROMConfig(unsigned char *buffer);
void increasePackDataCounts();
void increaseCurrentCounts();

/////////////////////////////////////////////////////////
// EEPROM global Function For I2C Slave to control
void setWriteBytesToEEPROM(unsigned int writeAdres, unsigned char byte_0, unsigned char byte_1);
void setReadBytesFromEEPROM(unsigned int writeAdres, unsigned char *buffer, unsigned char count);
void I2C_Slave_Access_EEPROM();


////////////////////////////////////////////////////////////////////////////
// Flash In-System Programming w/ write
// Information Flash Segment Access
////////////////////////////////////////////////////////////////////////////
//void WriteInitialDataToFlash(unsigned int Offset_Address, unsigned char *value, unsigned char dataLength);
//void ReadInitialDataFromFlash(unsigned int Offset_Address, unsigned char *value, unsigned char dataLength);
void WriteInitialDataToFlash(unsigned int Offset_Address, unsigned char *value, unsigned char dataLength, unsigned char SegmentSelection);
void WriteDataToFlash(unsigned int Offset_Address, unsigned char *value, unsigned char dataLength, unsigned char SegmentSelection);
void ReadInitialDataFromFlash(unsigned int Offset_Address, unsigned char *value, unsigned char dataLength, unsigned char SegmentSelection);
//void FlashWriteSeg(unsigned int Flash_Start_Address, char value);
//void FlashWriteData(unsigned int Flash_Start_Address, char *value, unsigned char dataLength);
void StartCalibration();
void setCalibrationData(char calibration_Point, char calibration_current_dir, unsigned int now_current_mA);
void CompleteCalibration();
//void InitialCalibrationDataToGlobalVar(char select);
void SaveOverCurrentCountingToFlash();
void ClearFlashAtOverCurrentCountingData();
